package com.isoftstone.app.controller.forOthers;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.mproject.MProject;
import com.isoftstone.merchant.entity.payment.Payment;
import com.isoftstone.merchant.service.mproject.impl.MProjectServiceImpl;
import com.isoftstone.merchant.service.payment.impl.PaymentServiceImpl;
import com.isoftstone.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhaojg
 * @Description
 * @Date: Created in 10:48 2017/12/15
 * @Modified by:
 * <p>
 * 为他人缴费接口
 */
@Controller
public class ForOthersController extends BaseController {

    @Autowired
    MProjectServiceImpl mProjectService;

    @Autowired
    PaymentServiceImpl paymentService;

    /**
     * @param corpId 缴费单位id
     * @return 缴费项目
     */
    @RequestMapping(value = "app/admin/queryProject", method = RequestMethod.POST)
    @ResponseBody
    public Object getPaymentProject(String corpId) {
//        corpId = "411";
        Tidings<Object> tidings = new Tidings<Object>();
        try {
            List<MProject> projects = mProjectService.getProjectByCorpId(corpId);
            tidings.setMsg("查询成功");
            tidings.setStatus(Const.SUCCESS);
            tidings.setT(projects);
        } catch (Exception e) {
            tidings.setMsg("查询失败");
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return tidings;
    }

    /**
     * 在该缴费单位下的缴费项目中根据缴费编号查询缴费详情
     *
     * @return
     */
    @RequestMapping(value = "app/admin/queryProjectDetail", method = RequestMethod.POST)
    @ResponseBody
    public Object queryProjectDetail(String corpId, String projectId, String feeId) {

        Tidings<Map<String, Object>> tidings = new Tidings<Map<String, Object>>();
        Map<String, Object> reMap = new HashMap<String, Object>();
        String status = Const.DJF;
        //根据缴费项目project_id和缴费编号和corpId查询
        List<Payment> paymentList = null;
        try {
            paymentList = paymentService.queryProjectDetail(corpId, projectId, feeId);
            //如果没有查到，则提示用户不存在
            if (paymentList.size() == 0) {
                tidings.setMsg("用户不存在");
                tidings.setStatus(Const.YHBCZ);
                return tidings;
            }
            long count = paymentList.stream().filter(payment -> payment.getPayStatus().equals(Const.YJF)).count();
            if (paymentList.size() == count) {
                tidings.setMsg("您当前没有未缴费项目");
                tidings.setStatus(Const.MYJFXM);
                return tidings;
            } else {
                List<Payment> paymentList1 = paymentService.queryProjectDetailByStatus(corpId, projectId, feeId, status);
                reMap.put("paymentList1",paymentList1);
                tidings.setMsg("有缴费项目");
                tidings.setStatus(Const.YJFXM);
                tidings.setT(reMap);
                return tidings;
            }
        } catch (Exception e) {
            tidings.setMsg("查询失败");
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return tidings;
    }
}
